﻿namespace HuaWei.DALFactory
{
    using HuaWei;
    using HuaWei.DAI;
    using System;
    using System.IO;
    using System.Reflection;

    public abstract class CDALFactory
    {
        private static IDataAccess idataAccess;

        protected CDALFactory()
        {
        }

        public static IDataAccess GetDALAccess()
        {
            string filename = string.Empty;
            string datasource = string.Empty;
            string dbname = string.Empty;
            string username = string.Empty;
            string password = string.Empty;
            filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System.ini");
            string dbtype = IniFile.ReadIniFile(filename, "SYS", "ConnType", false);
            if (dbtype.Equals(""))
            {
                dbtype = "MSSQLCe";
                dbname = "JLZFCore.dtm";
                username = "";
                password = "DB*((899";
                datasource = AppDomain.CurrentDomain.BaseDirectory;
            }
            else if (dbtype.ToUpper() == "MSSQL")
            {
                datasource = IniFile.ReadIniFile(filename, "DB", "DataSource", false);
                dbname = IniFile.ReadIniFile(filename, "DB", "DBName", false);
                username = IniFile.ReadIniFile(filename, "DB", "UserName", false);
                password = IniFile.ReadIniFile(filename, "DB", "Password", false);
            }
            else if (dbtype.ToUpper() == "MSSQLCE")
            {
                datasource = IniFile.ReadIniFile(filename, "DB", "DBPath", false);
                if (datasource.Equals(""))
                {
                    datasource = AppDomain.CurrentDomain.BaseDirectory;
                }
                dbname = IniFile.ReadIniFile(filename, "DB", "DBName", false);
                username = IniFile.ReadIniFile(filename, "DB", "UserName", false);
                password = IniFile.ReadIniFile(filename, "DB", "Password", false);
                if (password.Equals(""))
                {
                    password = "DB*((899";
                }
            }
            else if ((!"ORACLE".Equals(dbtype.ToUpper()) && !(dbtype.ToUpper() == "DB2")) && (dbtype.ToUpper() == "WCF"))
            {
                datasource = "";
            }
            idataAccess = GetDALAccess(dbtype, datasource, dbname, username, password);
            return idataAccess;
        }

        public static IDataAccess GetDALAccess(string dbtype, string datasource, string dbname, string username, string password)
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DA.dll");
            string name = string.Format("HuaWei.DAL.SqlClient.C{0}DataAccess", dbtype);
            string str3 = string.Empty;
            if (dbtype.ToUpper() == "MSSQL")
            {
                str3 = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", new object[] { datasource, dbname, username, password });
            }
            else if (dbtype.ToUpper() == "MSSQLCE")
            {
                str3 = string.Format("Data Source ={0}{1};Max Database Size=4090;Password={2}", datasource, dbname, password);
            }
            else if ("ORACLE".Equals(dbtype.ToUpper()))
            {
                str3 = string.Format("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT=1521))(CONNECT_DATA=(SERVICE_NAME={1})));\r\n                                         User Id={2};Password={3};", new object[] { datasource, dbname, username, password });
            }
            else if (dbtype.ToUpper() == "DB2")
            {
                str3 = string.Format("Server={0};Database={1};UID={2};PWD={3};", new object[] { datasource, dbname, username, password });
            }
            else if (dbtype.ToUpper() == "WCF")
            {
                str3 = string.Format(datasource, new object[0]);
                path = "DAWCF.dll";
            }
            IDataAccess access = null;
            if (!(string.IsNullOrEmpty(str3.Trim()) || !File.Exists(path)))
            {
                Assembly xx = Assembly.LoadFrom(path);
                Type type = xx.GetType(name);
                if (type != null)
                {
                    object obj2 = Activator.CreateInstance(type);
                    if (obj2 != null)
                    {
                        access = obj2 as IDataAccess;
                        access.DBType = dbtype;
                        access.ConnectionString = str3;
                        access.ProductNo = "JLSoft#FACEbook.61646#*((899";
                    }
                }
            }
            return access;
        }
    }
}

